package com.relsell.utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

/**
 * AppsVars class keeps application wide variables
 * 
 */

public class AppVars {
	public static boolean isLocal = false; // Please do not checkin it with true
											// value
	public static boolean isLogEnabled = true;
	public static boolean isDbBackUpEnabled = true;
	public static String LOG_TAG = "POSApplication";
	public static ArrayList<UserPin> sUserPinList = new ArrayList<UserPin>();
	public static ArrayList<UserPin> sFilteredUserPinList = new ArrayList<UserPin>();
	public static String sloggedInUserId;
	public static String sloggedInUserType;

	public static String getServer() {
		String server = null;
		if (!isLocal) {
			server = "khottal.com/relsell";
		} else {
			server = "192.168.1.8:9091";
		}
		return server;
	}

	public static void dbBackup(Context context) {
		try {
			File sd = Environment.getExternalStorageDirectory();
			File data = Environment.getDataDirectory();
			Log.v("TAG", "inside dbBackup");
			if (sd.canWrite()) {
				String currentDBPath = "/data/data/" + context.getPackageName()
						+ "/databases/relsell";
				String backupDBPath = "posdatabase.sqlite";
				File currentDB = new File(currentDBPath);
				File backupDB = new File(sd, backupDBPath);
				if (currentDB.exists()) {
					FileChannel src = new FileInputStream(currentDB)
							.getChannel();
					FileChannel dst = new FileOutputStream(backupDB)
							.getChannel();
					dst.transferFrom(src, 0, src.size());
					src.close();
					dst.close();
				}
			}
		} catch (Exception e) {

		}
	}

	public static void dbFromBackup(Context context) {
		try {
			File sd = Environment.getExternalStorageDirectory();
			File data = Environment.getDataDirectory();
			Log.v("TAG", "inside dbFromBackup");
			String currentDBPath = "posdatabase.sqlite";
			String backupDBPath = "/data/"+context.getPackageName()
					+ "/databases/relsell";
			File currentDB = new File(sd,currentDBPath);
			File backupDB = new File(data, backupDBPath);
			FileChannel src = new FileInputStream(currentDB).getChannel();
			FileChannel dst = new FileOutputStream(backupDB).getChannel();
			Log.v("TAG", "bytes transferred "+dst.transferFrom(src, 0, src.size()));
			
			src.close();
			dst.close();
		} catch (Exception e) {
			Log.v("TAG", "exception "+e);

		}
	}
}
